package com.liusaidh.service.impl;

import com.liusaidh.domain.Report;
import com.liusaidh.domain.User;
import com.liusaidh.result.Result;
import com.liusaidh.result.Status;
import com.liusaidh.dto.UpdateFormDTO;
import com.liusaidh.mapper.BlogMapper;
import com.liusaidh.mapper.UserMapper;
import com.liusaidh.service.MyService;
import com.liusaidh.utils.AesEncryptorUtil;
import com.liusaidh.utils.FileUtil;
import com.liusaidh.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author liusaidh
 * @date 2024/6/10 11:29
 * @description
 */
@Service
@Slf4j
public class MyServiceImpl implements MyService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private BlogMapper blogMapper;

    @Override
    public ModelAndView updateMyPassword(HttpSession session, String oldPwd, String newPwd, String confirmPwd) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("personalPage-pswdChange");
        if (RegexUtils.isPasswordInvalid(oldPwd) || RegexUtils.isPasswordInvalid(newPwd)) {
            modelAndView.addObject("result", Result.fail(Status.FAIL, oldPwd, "密码格式有误（8-16位至少一个大写字母）！"));
            return modelAndView;
        }
        if (!newPwd.equals(confirmPwd)) {
            modelAndView.addObject("result", Result.fail(Status.FAIL, oldPwd, "两次密码不一致！"));
            return modelAndView;
        }
        Integer id = (Integer) session.getAttribute("id");
        User user = userMapper.getUserById(id);
        String encryptOldPwd = AesEncryptorUtil.encrypt(oldPwd);
        if (!encryptOldPwd.equals(user.getPassword())) {
            modelAndView.addObject("result", Result.fail(Status.FAIL, oldPwd, "原密码不正确！"));
            return modelAndView;
        } else {
            String encryptNewPwd = AesEncryptorUtil.encrypt(newPwd);
            modelAndView.setViewName("login");
            userMapper.updateMyPwd(id, encryptNewPwd);
            modelAndView.addObject("result", Result.success(Status.OK, oldPwd, "修改成功"));
            return modelAndView;
        }
    }

    @Override
    public ModelAndView echoMyMessage(HttpSession session) {
        ModelAndView mav = new ModelAndView("personalPage-infoChange");
        User user = userMapper.getUserById((Integer) session.getAttribute("id"));
        mav.addObject("user", user);
        return mav;
    }

    @Override
    public ModelAndView updateMyInfo(HttpSession session, UpdateFormDTO userDTO, MultipartFile file) throws IOException {
        ModelAndView modelAndView = new ModelAndView("personalPage-infoChange");
        Integer id = (Integer) session.getAttribute("id");
        modelAndView.addObject("user", userMapper.getUserById(id));
        User user = userMapper.getUserById(id);
        String url;
        if (file == null) {
            url = user.getAvatarUrl();
        } else {
            if (!FileUtil.isImage(file)) {
                modelAndView.addObject("result", Result.fail(Status.FILE_IS_ERROR));
                return modelAndView;
            }
            url = FileUtil.uploadOne(file);
        }
        userMapper.updateUserInfo(id, url, userDTO);
        modelAndView.addObject("user", userMapper.getUserById(id));
        modelAndView.addObject("result", Result.success(Status.OK,"修改成功"));
        return modelAndView;
    }

    @Override
    public ModelAndView getReportForUser(Integer id, Integer pageNo, Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView("tip-off");
        modelAndView.addObject("reportList",blogMapper.getReportForUser(id, (pageNo - 1) * pageSize, pageSize));
        return modelAndView;
    }

    @Override
    public List<Report> getReportListForUser(Integer id, Integer pageNo, Integer pageSize) {
        return blogMapper.getReportForUser(id, (pageNo - 1) * pageSize, pageSize);
    }
}
